﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using SupplyChain.Domain.Entities;
using SupplyChain.ErrorCode;
using SupplyChain.Write.API.Application.Command.PurchaseOrder;
using SupplyChain.Write.API.Application.Command.Supplier;

namespace SupplyChain.Write.API.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class SupplierController : ControllerBase
    {
        private readonly IMediator mediator;

        public SupplierController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 供应商信息添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("AddSupplierProfile")]
        public async Task<ApiResponse<bool>> AddSupplierProfile(AddSupplierProfileCommand command)
        {
            if (await mediator.Send(command))
            {
                return ApiResponse<bool>.Success(true, "操作成功");
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 获取供应商列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetSupplierProfile")]
        public async Task<ApiResponse<List<SupplierProfile>>> GetSupplierProfile()
        {
            return ApiResponse<List<SupplierProfile>>.Success(await mediator.Send(new GetSupplierProfileCommand()));
        }

        /// <summary>
        /// 删除供应商
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("DelSupplierProfile")]
        public async Task<ApiResponse<bool>> DelSupplierProfile([FromQuery] DelSupplierProfileCommand command)
        {
            if (await mediator.Send(command))
            {
                return ApiResponse<bool>.Success(true, "操作成功");
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 修改供应商信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        [Route("UpdSupplierProfile")]
        public async Task<ApiResponse<bool>> UpdSupplierProfile(UpdSupplierProfileCommand command)
        {
            if (await mediator.Send(command))
            {
                return ApiResponse<bool>.Success(true, "操作成功");
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 供应商类型添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("AddSupplierCategory")]
        public async Task<ApiResponse<bool>> AddSupplierCategory(AddSupplierCategoryCommand command)
        {
            if (await mediator.Send(command))
            {
                return ApiResponse<bool>.Success(true, "操作成功");
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 获取供应商类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetSupplierCategory")]
        public async Task<ApiResponse<List<SupplierCategory>>> GetSupplierCategory()
        {
            return ApiResponse<List<SupplierCategory>>.Success(await mediator.Send(new GetSupplierCategoryCommand()));
        }

        /// <summary>
        /// 删除供应商类型
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("DelSupplierCategory")]
        public async Task<ApiResponse<bool>> DelSupplierCategory(DelSupplierCategoryCommand command)
        {
            if (await mediator.Send(command))
            {
                return ApiResponse<bool>.Success(true, "操作成功");
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 修改供应商类型
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        [Route("UpdSupplierCategory")]
        public async Task<ApiResponse<bool>> UpdSupplierCategory(UpdSupplierCategoryCommand command)
        {
            if (await mediator.Send(command))
            {
                return ApiResponse<bool>.Success(true, "操作成功");
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }
    }
}
